package com.nit.lab.controller.ship;

import com.alibaba.fastjson.JSONObject;

import com.nit.lab.dto.ship.ShipDTO;
import com.nit.lab.dto.system.TokenDTO;
import com.nit.lab.dto.utils.OpResultDTO;
import com.nit.lab.dto.ship.ShipTrackDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.service.ship.ShipTrackService;
import com.nit.lab.utils.HttpRequestReader;
import com.nit.lab.utils.JWTUtil;
import com.nit.lab.utils.Jpython;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/api/shipTrack")
public class ShipTrackController {

    @Resource
    private ShipTrackService shipTrackService;
    // 使用指定的类XXX初始化日志对象，方便在日志输出的时候，可以打印出日志信息所属的类。
    private final Logger logger = LoggerFactory.getLogger(ShipTrackController.class);


    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public OpResultDTO query(HttpServletRequest request,@RequestHeader(name = "Access-Token") String token) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            JSONObject jsonObject = HttpRequestReader.getJsonObject(request);
            TableReqDTO tableReqDTO = new TableReqDTO();
            tableReqDTO.setQueryParam(jsonObject.getString("queryParam"));
            String mmsi =  tableReqDTO.parseQueryParam("mmsiNumber");
            opResult.setMessage("success");
            opResult.setResult(shipTrackService.query(mmsi));
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
            opResult.setResult("获取船舶轨迹失败");
        }
        return opResult;
    }

    @RequestMapping(value = "search", method = RequestMethod.POST)
    public OpResultDTO search(@RequestHeader(name = "Access-Token") String token,@RequestBody ShipTrackDTO shipTrackDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            ShipTrackDTO tempShip = shipTrackService.queryMinTime(shipTrackDTO.getMmsiNumber(),shipTrackDTO.getPortChinese());
            opResult.setMessage("success");
            if (null == tempShip) {
                opResult.setResult(shipTrackService.query(shipTrackDTO.getMmsiNumber()));
            } else {
                opResult.setResult(shipTrackService.queryLaterShip(shipTrackDTO.getMmsiNumber(),tempShip.getMinTime()));
            }

        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
            opResult.setResult("查询船舶轨迹失败");
        }
        return opResult;
    }

    /**
     * @Description: python接口
     * @Author: zmh
     * @Date: 2021/02/14 14:52
     * HttpServletRequest对象代表客户端的请求，当客户端通过HTTP协议访问服务器时，HTTP请求头中的所有信息都封装在这个对象中，通过这个对象提供的方法，可以获得客户端请求的所有信息。
     * RequestMapping是一个用来处理请求地址映射的注解，可用于类或方法上。用于类上，表示类中的所有响应请求的方法都是以该地址作为父路径。
     */
    @RequestMapping(value = "/pythonApi", method = RequestMethod.POST)
    public OpResultDTO jPython(HttpServletRequest request,@RequestHeader(name = "Access-Token") String token) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            JSONObject jsonObject = HttpRequestReader.getJsonObject(request);
            TableReqDTO tableReqDTO = new TableReqDTO();
            tableReqDTO.setQueryParam(jsonObject.getString("queryParam"));
            Jpython jpython = new Jpython();
            String mmsi = tableReqDTO.parseQueryParam("mmsiNumber");
            String beginTime = tableReqDTO.parseQueryParam("beginTime");
            String endTime = tableReqDTO.parseQueryParam("endTime");
            String btime = tableReqDTO.getTimeStamp(beginTime);
            String etime = tableReqDTO.getTimeStamp(endTime);
            String res = jpython.pythonApi(mmsi,btime,etime);
            opResult.setResult(res);
            opResult.setResult(shipTrackService.list4SpecificShip(mmsi,btime,etime));
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
            opResult.setResult("获取船舶轨迹失败");
        }
        return opResult;
    }





}
